package com.pan.controller;


import com.pan.entity.Result;
import com.pan.service.AdminService;
import com.pan.vo.AdminRegisterVo;
import com.pan.vo.AdminVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;


    @ResponseBody
    @PostMapping("/login")
    public Result<String> login(HttpServletRequest request, HttpServletResponse response,
                                @RequestParam Long tel, @RequestParam String password){
        Result<String> result = adminService.toLogin(request, response, tel, password);
        return result;
    }

    @ResponseBody
    @PostMapping("/logoff")
    public Result logoff(HttpServletResponse response,
                         @CookieValue("adminTicket") String adminTicket){
        Result result = adminService.logoff(response, adminTicket);
        return result;
    }

    @ResponseBody
    @PostMapping("/register")
    public Result register(@CookieValue("adminTicket") String adminTicket,
                           @RequestBody AdminRegisterVo adminRegisterVo){
        Result result = adminService.register(adminTicket,adminRegisterVo);
        return result;
    }

    @ResponseBody
    @GetMapping("/select/tel/{tel}")
    public Result<AdminVo> selectByTel(@PathVariable Long tel,
                                      @CookieValue("adminTicket") String adminTicket){
        Result<AdminVo> result = adminService.selectByTel(adminTicket,tel);
        return result;
    }
    @ResponseBody
    @PostMapping("/update/password")
    public Result updatePassword(@CookieValue("adminTicket") String adminTicket,
                                 @RequestParam("oldPassword") String oldPassword,
                                 @RequestParam("newPassword") String newPassword)
            throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        Result result = adminService.updatePassword(adminTicket, oldPassword, newPassword);
        return result;

    }
    @ResponseBody
    @PostMapping("/update/{memberName}")
    public Result update(@CookieValue("adminTicket") String adminTicket,
                         @PathVariable String memberName,
                         @RequestParam Object value)
            throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        Result result = adminService.updateInfo(adminTicket, memberName, value);
        return result;
    }

}
